package com.basic.business.artErp.controller;

import com.basic.business.artErp.domain.TArtErp;
import com.basic.business.artErp.service.ITArtErpService;
import com.basic.business.mat.domain.TMatMgmt;
import com.basic.business.quantityReceived.domain.TQuantityReceived;
import com.basic.business.quantityReceived.service.ITQuantityReceivedService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 任务erpController
 *
 * @author ruize
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/business/taskerp")
public class TArtErpController extends BaseController
{
    @Resource
    private ITArtErpService tArtErpService;
    @Resource
    private ITQuantityReceivedService tQuantityReceivedService;

    /**
     * 查询任务erp列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TArtErp tArtErp)
    {
        startPage();
        List<TArtErp> list = tArtErpService.getList(tArtErp);
        return getDataTable(list);
    }

    /**
     * 导出任务erp列表
     */
    @Log(title = "任务erp", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "任务ERP数据.xlsx")
    @PostMapping("/export")
    public String export(TArtErp tArtErp) throws IOException
    {
        List<TArtErp> list = tArtErpService.getList(tArtErp);
        return EasyExcelUtils.excelExport(TArtErp.class,"全站推指标数据数据", list);
    }

    /**
     * 获取任务erp详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tArtErpService.get(id));
    }

    /**
     * 新增任务erp
     */
    @Log(title = "任务erp", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TArtErp tArtErp)
    {
        return toAjax(tArtErpService.insertTArtErp(tArtErp));
    }

    /**
     * 修改任务erp
     */
    @Log(title = "任务erp", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TArtErp tArtErp)
    {
        return toAjax(tArtErpService.updateTArtErp(tArtErp));
    }

    @PutMapping(value = "/receivingEdit")
    public AjaxResult receivingEdit(@RequestBody TArtErp tArtErp)
    {
        List<TArtErp> list = tArtErpService.getretrieve(tArtErp);
        if (list.size() == 0){
            TQuantityReceived tQuantityReceived = new TQuantityReceived();
            TArtErp tArtErps = new TArtErp();

            // 根据不同的条件设置tQuantityReceived的userId和tArtErps的相关字段
            if ("1".equals(tArtErp.getStatus()) && tArtErp.getReceiveTasksPerson() != null && !tArtErp.getReceiveTasksPerson().isEmpty()) {
                tQuantityReceived.setUserId(Long.valueOf(tArtErp.getReceiveTasksPerson()));
                tArtErps.setReceiveTasksPerson(tArtErp.getReceiveTasksPerson());
            } else if ("7".equals(tArtErp.getStatus()) && tArtErp.getArtistAllocation() != null && !tArtErp.getArtistAllocation().isEmpty()) {
                tQuantityReceived.setUserId(Long.valueOf(tArtErp.getArtistAllocation()));
                tArtErps.setArtistAllocation(tArtErp.getArtistAllocation());
            } else if ("2".equals(tArtErp.getMakeStatus()) && tArtErp.getMake() != null && !tArtErp.getMake().isEmpty()) {
                tQuantityReceived.setUserId(Long.valueOf(tArtErp.getMake()));
                tArtErps.setMake(tArtErp.getMake());
            } else if ("13".equals(tArtErp.getStatus()) && tArtErp.getSku() != null && !tArtErp.getSku().isEmpty()) {
                tQuantityReceived.setUserId(Long.valueOf(tArtErp.getSku()));
                tArtErps.setSku(tArtErp.getSku());
            }

            int artNum = tArtErpService.selectArtNum(tArtErps);
            List<TQuantityReceived> num = tQuantityReceivedService.selectList(tQuantityReceived);
            if(tArtErp.getMake() != null && !tArtErp.getMake().isEmpty()){
                tArtErp.setMakeStatus("2");
            }
            if (num.size() == 0){
                if (artNum < 5){
                    return toAjax(tArtErpService.updateTArtErp(tArtErp));
                }else {
                    return error("接取上限！");
                }
            }else {
                if (artNum < num.get(0).getNum()){
                    return toAjax(tArtErpService.updateTArtErp(tArtErp));
                }else {
                    return error("接取上限！");
                }
            }
        }else {
            return error("接取失败！");
        }
    }

    /**
     * 删除任务erp
     */
    @Log(title = "任务erp", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tArtErpService.deleteByIds(ids));
    }

    @GetMapping("/getUserIdList")
    public AjaxResult getUserIdList()
    {
        List<Map<String, Object>> list = tArtErpService.getUserIdList();
        return success(list);
    }

    @GetMapping("/getTaskCompletionStatistics")
    public AjaxResult getTaskCompletionStatistics(@RequestParam(value = "beginTime", required = false) String beginTime, @RequestParam(value = "endTime", required = false) String endTime, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "userId", required = false) String userId)
    {
        List<Map<String, Object>> list = tArtErpService.getTaskCompletionStatistics(beginTime, endTime, name, userId);
        return success(list);
    }

    @GetMapping("/getDoesItExist")
    public AjaxResult getDoesItExist(TArtErp tArtErp)
    {
        List<TArtErp> list = tArtErpService.getDoesItExist(tArtErp);
        return success(list);
    }

    @GetMapping("/getAssignTasksList")
    public AjaxResult getAssignTasksList(TArtErp tArtErp)
    {
        List<TArtErp> list = tArtErpService.getAssignTasksList(tArtErp);
        return success(list);
    }

    @GetMapping("/getReceiveTasksPersonList")
    public AjaxResult getReceiveTasksPersonList()
    {
        List<Map<String, Object>> list = tArtErpService.getReceiveTasksPersonList();
        return success(list);
    }

    @GetMapping("/getMakeUpList")
    public AjaxResult getMakeUpList()
    {
        List<Map<String, Object>> list = tArtErpService.getMakeUpList();
        return success(list);
    }

    @GetMapping("/getArtistAllocationList")
    public AjaxResult getArtistAllocationList()
    {
        List<Map<String, Object>> list = tArtErpService.getArtistAllocationList();
        return success(list);
    }
}
